<template>
    <el-dialog
        v-model="configCenterRefreshDialogVisible"
        title="刷新配置"
        width="30%"
        align-center
        :show-close="false"
        :close-on-click-modal="false"
    >
        <div class="formContainer">
            <el-form :model="configCenterForm" :rules="configCenterRules" ref="configCenterFormRef">
                <el-form-item label="所属微服务" label-width="20%" prop="application">
                    <el-select v-model="configCenterForm.application" clearable placeholder="所属微服务" style="width: 100%">
                        <el-option :value="item.applicationName" :label="item.applicationName" v-for="item in applicationList" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="cancelDialog()">取 消</el-button>
                <el-button type="primary" :loading="saveLoading" @click="submitDialog(configCenterFormRef)">
                    刷新配置
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script name="ServiceRefreshConfigCenterDialog" setup lang="ts">
import {applicationInfoQuery, applicationNacosInfoQuery} from "@/request/service";
import {refreshConfigInfo} from "@/request/configCenter";
import {ElNotification, FormInstance} from "element-plus";
import {computed, ref} from "vue";
import {useServiceConfigStore} from "@/stores/serviceManage/serciceConfig";

let configCenterForm = ref({
    application: ''
});
let configCenterRules = ref({
    application: [{ required: true, message: '所属微服务不能为空', trigger: 'blur' }]
});
let applicationList = ref([]);
let saveLoading = ref(false);

let configCenterRefreshDialogVisible = computed(() => {return useServiceConfigStore().refreshDialog});

// 查询应用列表
applicationInfoQuery({}).then(res => {
    if (res['code'] === '200') {
        applicationList.value = res.data
    }
})

function cancelDialog() {
    useServiceConfigStore().resetRefreshDialog(false);
}

let configCenterFormRef = ref<FormInstance>();

function submitDialog(formEl: FormInstance | undefined) {
    if (!formEl) return
    saveLoading.value = true
    formEl.validate((valid) => {
        if (valid) {
            // 查询当前选中的微服务的实例个数
            applicationNacosInfoQuery({applicationName: configCenterForm.value.application}).then(res => {
                if (res["code"] === '200') {
                    let serviceInstance = res.data
                    let paramDataInfo = {
                        application: configCenterForm.value.application
                    }
                    for (let i = 0; i < serviceInstance.length; i++) {
                        refreshConfigInfo(paramDataInfo).then(res => {
                            if (res["code"] !== '200') {
                                ElNotification({
                                    title: 'Error',
                                    message: res["message"],
                                    type: 'error'
                                })
                                return;
                            }
                        })
                    }
                    ElNotification({
                        title: 'Success',
                        message: res["message"],
                        type: 'success'
                    })
                    useServiceConfigStore().resetRefreshDialog(false);
                }
                saveLoading.value = false
            }).catch(res => {
                saveLoading.value = false
            })
        }
    })
}
</script>

<style scoped>

</style>